﻿using CommonLibrary;
using CommonLibrary.Models;
using CommonSite.Areas.API.Models;
using CommonSite.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLToolkit.Data.Linq;
using CommonHelper;

namespace CommonSite.Areas.API.Controllers
{
    public class OrgController : BaseAPIController
    {
        public ActionResult Query()
        {
            using (CommonDatabase db = new CommonDatabase())
            {
                var query = (from p in db.sys_Org
                             where p.State == 1
                             select new Org
                             {
                                 Id = CryptHelper.Encrypt(p.Id),
                                 ParentOrgId = p.ParentOrgId == null ? null : CryptHelper.Encrypt(p.ParentOrgId),
                                 OrgName = p.Name,
                                 ParentOrgName = p.ParentOrg.Name
                             }).ToList();

                return Success(new
                {
                    Total = query.Count,
                    Rows = query
                });
            }
        }

        public ActionResult Create(string OrgName, string ParentOrgId)
        {
            if (!OrgName.IsNullOrWhiteSpace())
            {
                try
                {
                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        int? parent = null;
                        if (!ParentOrgId.IsNullOrWhiteSpace())
                        {
                            parent = CryptHelper.DecryptToInt32(ParentOrgId);
                        }

                        db.sys_Org.Insert(() => new sys_Org
                        {
                            Name = OrgName,
                            ParentOrgId = parent
                        });

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Update(Org Org)
        {
            if (this.ModelState.IsValid)
            {
                try
                {
                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        int id = CryptHelper.DecryptToInt32(Org.Id);
                        db.sys_Org.Where(k => k.Id == id)
                          .Set(k => k.Name, Org.OrgName)
                          .Update();

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Delete(Org Org)
        {
            try
            {
                using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                {
                    db.BeginTransaction();
                    int id = CryptHelper.DecryptToInt32(Org.Id);
                    if (db.sys_Org.Where(k => k.Id == id && Org.OrgName == k.Name)
                          .Count() == 0)
                    {
                        throw new ArgumentException("职位不存在");
                    }

                    db.sys_UserOrgPosition.Where(k => k.OrgId == id)
                      .Delete();
                    db.sys_Org.Where(k => k.Id == id)
                      .Set(k=>k.State, 0)
                      .Update();

                    db.CommitTransaction();
                    return Success();
                }
            }
            catch (Exception ex)
            {
                return Failed(ex.Message);
            }
        }
    }
}
